package cn.lg.soar.database.datascope;

import cn.lg.soar.database.annotations.DataScope;
import cn.lg.soar.database.annotations.DataScopeIgnore;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

/**
 * @author luguoxiang 469599794@qq.com
 * @Date: 2024/3/1 21:59
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ConditionalOnProperty(name = "lg.database.enabled-data-permission", havingValue = "true", matchIfMissing = true)
@Aspect
public class DataScopeAspect {

    @Around("@annotation(dataResource)")
    public Object resource(ProceedingJoinPoint point, DataScope dataResource) throws Throwable {
        DataScopeHolder.setResourceCode(dataResource.value());
        try {
            return point.proceed();
        } finally {
            DataScopeHolder.delResourceCode();
        }
    }

    @Around("@annotation(dataScopeIgnore)")
    public Object ignore(ProceedingJoinPoint point, DataScopeIgnore dataScopeIgnore) throws Throwable {
        DataScopeHolder.addIgnore();
        try {
            return point.proceed();
        } finally {
            DataScopeHolder.delIgnore();
        }
    }

}
